package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.ConfigEnum;
/**
 * 设备参数频率设置DAO
 * @author chengshichao
 * 创建时间：2013-3-22
 */
public class VclParaFreqSetDAO {
	
	private IBaseDAO baseDAO;
	
	private ITransactionDAO tranDAO;
	/**
	 * @return the baseDAO
	 */
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}
	/**
	 * @param baseDAO the baseDAO to set
	 */
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	/**
	 * @return the tranDAO
	 */
	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}
	/**
	 * @param tranDAO the tranDAO to set
	 */
	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}
	
	/**
	 * 到期的专有参数频率设定信息
	 */
	public List<Map<String,Object>>  getValidParaFreqUDS(){
		String strSQL = "select * from VclParaFreqInfo " +
				"where GETDATE() between VclPFI_Eff and VclPFI_Exp and VclPFI_ValidDateUDS<CONVERT(varchar(10),GETDATE(),120) " +
				"and VclPFI_ChkStatusUDS=0";
		List<Map<String,Object>> list = baseDAO.execQuery(strSQL, new Object[]{});
		return list;
	}
	
	/**
	 * 到期的公有参数频率设定信息
	 */
	public List<Map<String,Object>>  getValidParaFreqPGN(){
		String strSQL = "select * from VclParaFreqInfo " +
				"where GETDATE() between VclPFI_Eff and VclPFI_Exp and VclPFI_ValidDatePGN<CONVERT(varchar(10),GETDATE(),120) " +
				"and VclPFI_ChkStatusPGN=0";
		List<Map<String,Object>> list = baseDAO.execQuery(strSQL, new Object[]{});
		return list;
	}
	
	/**
	 * 更新到期的专有参数检索状态
	 * @return
	 */
	public boolean updateChkStatusUDS(){
		String strSQL = "update VclParaFreqInfo set VclPFI_ChkStatusUDS=1 " +
			"where GETDATE() between VclPFI_Eff and VclPFI_Exp and VclPFI_ValidDateUDS<CONVERT(varchar(10),GETDATE(),120) " +
			"and VclPFI_ChkStatusUDS=0";
		int iFlag = baseDAO.execUpdate(strSQL, new Object[]{});
		if(iFlag>0)return true;
		return false;
	}
	
	/**
	 * 更新到期的公有参数检索状态
	 * @return
	 */
	public boolean updateChkStatusPGN(){
		String strSQL = "update VclParaFreqInfo set VclPFI_ChkStatusPGN=1 " +
			"where GETDATE() between VclPFI_Eff and VclPFI_Exp and VclPFI_ValidDatePGN<CONVERT(varchar(10),GETDATE(),120) " +
			"and VclPFI_ChkStatusPGN=0";
		int iFlag = baseDAO.execUpdate(strSQL, new Object[]{});
		if(iFlag>0)return true;
		return false;
	}

	/**
	 * 设置终端频率
	 * @param vclList 设置频率的设备集合
	 * @param setParaType 参数类型 ，0：专有      1：公有
	 * @param freVal 设置频率值，单位：秒
	 * @return
	 */
	public boolean setTmnlFreqInfo(List<Map<String,Object>> vclList,String setParaType,String freVal){
		if(!StringUtil.isNullOrEmpty(freVal)||!StringUtil.isNullOrEmpty(setParaType))return false;
		String columnName = "";
		if("0".equals(setParaType)){
			columnName = "MsgTlS_UDSCyc";
		}else if("1".equals(setParaType)){
			columnName = "MsgTlS_PGNCyc";
		}else{
			//暂时未有其他参数类型
		}
		if(!StringUtil.isNullOrEmpty(columnName))return false;
		try{
			List<String> sqlList = new ArrayList<String>();
			for(Map<String,Object> vclMap:vclList){
				String sql = "insert into " +GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set","3",null)+
						"(MsgTlS_Vcl_ID,MsgTlS_SetTime,MsgTlS_TimeOver,MsgTlS_SysCT_ID,MsgTlS_SysCS_ID,"+columnName+")" +
						"values("+MapGetter.getString(vclMap, "VehicleID")+",GETDATE(),0,"+ConfigEnum.CommType_ZX.getName()+","+ConfigEnum.CmcState_Submit.getName()+","+freVal+");";
				sqlList.add(sql);
			}
			return tranDAO.batch(sqlList);
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
}
